const tmplAvatar = window.templates['avatar'];

/* Avatar colors, as CSS colors */
const AVATAR_COLORS = ['red', 'orange', 'green', 'blue', 'pink']
    .map(color => `var(--${color}-color)`);

const getAvatarURL = function(details) {
    if (details.hasIcon) {
        return window.BASE_ICON + encodeURIComponent(details.id);
    } else {
        return null;
    }
};

/* Returns hash of user as single number */
const getAvatarHash = function(details) {
    return details.id.split('').map(c => c.charCodeAt(0)).reduce((a, b) => a + b, 0);
};

window.getAvatar = function(details) {
    return tmplAvatar({
        url: getAvatarURL(details),
        initial: details.alias ? details.alias[0] : '?',
        color: AVATAR_COLORS[getAvatarHash(details) % AVATAR_COLORS.length]
    });
};
